package org.xmatch.framework.presentation;

import java.util.List;

import javax.faces.component.UIData;

import org.xmatch.framework.business.model.GenericBusinessObject;
import org.xmatch.framework.entity.GenericEntity;
import org.xmatch.framework.exception.GenericException;


public abstract class GenericEntityListAction<E extends GenericEntity> extends
		GenericListAction<E> {

	private static final long serialVersionUID = -2120661013301513730L;
	protected GenericBusinessObject<E> businessService;


	protected void initResultList() throws GenericException {
		List<E> list = getBusinessService().findByParameters(getInstance(),
				getOrder(), isAsc());
		setResultList(list);
	}

	
	@Override
	protected int initPagedResult() throws GenericException {

		UIData pagedDataTable = getDataTable();

		List<E> list = getBusinessService().findByParameters(getInstance(),
				getOrder(), isAsc(), pagedDataTable.getFirst(),
				pagedDataTable.getRows());
		setPagedResult(new GenericPagedDataModel(list, getResultCount(),
				list != null ? list.size() : 0));
		return pagedDataTable.getFirst();
	}

	
	public List<E> findAll() throws GenericException {
		
		return businessService.findAll();
	}

	@Override
	protected void initResultCount() throws GenericException {
		resultCount = getBusinessService().getDatasetSize(getInstance());
	}


	protected GenericBusinessObject<E> getBusinessService() {
		return businessService;
	}

}